package servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import utils.db.impl.EditDevice;

/**
 * Servlet implementation class DeleteDeviceServlet
 */
@WebServlet("/DeleteDeviceServlet")
public class DeleteDeviceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	HttpSession session;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("delete"));
		
		if(id!=0){
			EditDevice edit = new EditDevice();
			edit.deleteDevice(id);
			
			session = request.getSession(true);
			session.setAttribute("delete", "Urządzenie zostało usunięte!");
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "index.jsp?page=adminPanel#pagsd");
		}else{
			session = request.getSession(true);
			session.setAttribute("delete", "Problem przy usuwaniu urządzenia!");
			response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
			response.setHeader("Location", "index.jsp?page=adminPanel#pagsd");
		}
	}

}
